/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package InternetHax;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author LoginError
 */
public class ColorMenu extends Menu{
    int[] rgb = new int [3];
    int finalColor = 0;
    int selectedIndex = 0;
    int maxIndex = 3;

    synchronized private void intToRGB()
    {
        int r,g,b;

        r = finalColor & 0x0000FF;
        g = finalColor & 0x00FF00;
        b = finalColor & 0xFF0000;

        rgb[2] = r;
        rgb[1] = g >> 8;
        rgb[0] = b >> 16;
    }

    synchronized private void rgbToInt()
    {
        int g,b;

        g = rgb[1];
        b = rgb[0];

        g = (g << 8);
        b = (b << 16);

        finalColor = rgb[2];
        finalColor += g;
        finalColor += b;
    }

    synchronized public void indexPlus() {
        if (++selectedIndex >= maxIndex) {
            selectedIndex = maxIndex;
        }
        
    }

    synchronized public void indexMinus() {
        if (--selectedIndex < 0) {
            selectedIndex = 0;
        }
    }

    private void alterValue(int altervalue)
    {
        if(selectedIndex == 3)
            return;
        else
            rgb[selectedIndex] += altervalue;

        if(rgb[selectedIndex] > 255)
            rgb[selectedIndex] = 255;
        if(rgb[selectedIndex] < 0)
            rgb[selectedIndex] = 0;

        rgbToInt();
    }


    synchronized public int getColor(int color)
    {
        finalColor = color;
        intToRGB();

        Gamestate.modalMenu = this;
        try {
            this.wait();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        Gamestate.modalMenu = null;
        return finalColor;
    }


    synchronized public void paint(Graphics g, int x, int y) {
     int width, height;
        width = 150;
        height = 100;

        g.setColor(Gamestate.uiDetailColor);
        g.fillRect(x - width / 2, y - height / 2, width, height);
        g.setColor(Gamestate.uiBackgroundColor);
        g.fillRect(x - width / 2 + 1, y - height / 2 + 1, width - 2, height - 2);

        int offset = 32;

        Gamestate.font.PrintString(g, x - width / 2 + 3, y - height / 2 + 3, "Color picker", 0, 0, Gamestate.fontColor); // print the title

        g.setColor(0xFFFFFF);
        g.drawRect(x - width/2 + 10, y - height / 2 + 15, 130, 15);

        g.setColor(finalColor);
        g.fillRect(x - width/2 + 11, y - height / 2 + 16, 129, 14);

        for (int i = 0 ; i < 4; i++) {
            
            if (i == selectedIndex) {
                g.setColor(Gamestate.uiDetailColor);
                g.drawRect(x - width / 2 + 2, y - height / 2 + 2 + offset, width - 10, 9);
            }

            if(i == 0)
            {
                Gamestate.font.PrintString(g, x - width / 2 + 3, y - height / 2 + 3 + offset, "R: < " + rgb[0] + " >", 0, 0, Gamestate.fontColor);
            }
            if(i == 1)
            {
                Gamestate.font.PrintString(g, x - width / 2 + 3, y - height / 2 + 3 + offset, "G: < " + rgb[1] + " >", 0, 0, Gamestate.fontColor);
            }
            if(i == 2)
            {
                Gamestate.font.PrintString(g, x - width / 2 + 3, y - height / 2 + 3 + offset, "B: < " + rgb[2] + " >", 0, 0, Gamestate.fontColor);
            }
            if(i == 3)
            {
                Gamestate.font.PrintString(g, x - width / 2 + 3, y - height / 2 + 3 + offset, "Done", 0, 0, Gamestate.fontColor);
            }
            
            offset += 10;
        }
    }

   synchronized public void handleInput(int keyAction) {
       switch(keyAction)
       {
           case HaxCanvas.DOWN:
             indexPlus();
           break;

           case HaxCanvas.UP:
             indexMinus();
           break;

           case HaxCanvas.LEFT:
             alterValue(-5);
           break;

           case HaxCanvas.RIGHT:
             alterValue(5);
           break;

           case HaxCanvas.FIRE:
               this.notify();
           break;
       }
    }

}
